﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sdk_cop
{
    public class UiIcbcClient:DefaultIcbcClient
    {
        /**
	     * 采用工行的CA证书进行签名，证书为工行颁发
	     * 
	     * @param appId
	     *            应用编号
	     * @param privateKey
	     *            CA证书的私钥
	     * @param charset
	     *            传输过程中的编码
	     * @param ca
	     *            CA公钥证书
	     */
        public UiIcbcClient(string appId, string privateKey, string charset, string ca, string password):
            base(appId, IcbcConstants.SIGN_TYPE_CA, privateKey, charset, IcbcConstants.FORMAT_JSON, null, null, null, ca,
                    password)
        {
            
        }
        /**
	     * 采用公私钥对方式进行签名，公私钥为用户生成，并提交公钥至工商银行API开放平台
	     * 
	     * @param appId
	     *            应用编号
	     * @param privateKey
	     *            应用私钥
	     * @param charset
	     *            传输过程中的编码
	     */
        public UiIcbcClient(string appId, string privateKey, string charset):
            base(appId, IcbcConstants.SIGN_TYPE_RSA, privateKey, charset, IcbcConstants.FORMAT_JSON, null, null, null,
                    null, null)
        {
            
        }
        /**
	     * 采用公私钥对方式进行签名，公私钥为用户生成，并提交公钥至工商银行API开放平台
	     * 
	     * @param appId
	     *            应用编号
         * @param signType
         *            签名类型
	     * @param privateKey
	     *            应用私钥
	     * @param charset
	     *            传输过程中的编码
	     */
        public UiIcbcClient(string appId, string signType, string privateKey, string charset):
            base(appId, signType, privateKey, charset, IcbcConstants.FORMAT_JSON, null, null, null,
                    null, "")
        {
            
        }
        /**
	     * 采用公私钥对方式进行签名，公私钥为用户生成，并提交公钥至工商银行API开放平台
	     * 
	     * @param appId
	     *            应用编号
         * @param signType
	     *            签名类型
	     * @param privateKey
	     *            应用私钥
         * @param encryptType
	     *            加密类型
	     * @param encryptKey
	     *            加密密钥        
	     * @param charset
	     *            传输过程中的编码
	     */
        public UiIcbcClient(string appId, string signType,string privateKey, string encryptType, string encryptKey, string charset) :
            base(appId, signType, privateKey, charset, IcbcConstants.FORMAT_JSON, null,encryptType, encryptKey,
                     null, null)
        {

        }
        public string buildPostForm<T>(AbstractIcbcRequest<T> request) where T : IcbcResponse 
        {
		    string uuid = Guid.NewGuid().ToString();
            string msgId = uuid.Replace("-", "");
		    return buildPostForm(request, msgId, null);
	    }

	    public string buildPostForm<T>(AbstractIcbcRequest<T> request, string msgId) where T : IcbcResponse
        {
		    return buildPostForm(request, msgId, null);
	    }
        public string buildPostForm<T>(AbstractIcbcRequest<T> request, string msgId, string appAuthToken) where T : IcbcResponse
        {

		    Dictionary<string,string> param = prepareParams(request, msgId, appAuthToken);

		    Dictionary<string,string> urlQueryParams = buildUrlQueryParams(param);
		    string url = WebUtils.buildGetUrl(request.getServiceUrl(), urlQueryParams, charset);

		    return WebUtils.buildForm(url, buildBodyParams(param));

	    }

        protected Dictionary<string,string> buildUrlQueryParams(Dictionary<string,string> param) 
        {
		    Dictionary<string,string> urlQueryParams = new Dictionary<string,string>();
            foreach (var s in param)
            {
                string key = s.Key;
                string value = s.Value;
                if(HttpRequestParamsUtils.apiParamNames.Contains(key))
                {
                    urlQueryParams.Add(key,value);
                }
            }
		    return urlQueryParams;
	    }

        protected Dictionary<string,string> buildBodyParams(Dictionary<string,string> param) 
        {
		    Dictionary<string,string> urlQueryParams = new Dictionary<string,string>();

            foreach(var s in param)
            {
                string key = s.Key;
                string value = s.Value;
                if(HttpRequestParamsUtils.apiParamNames.Contains(key))
                    continue;
                urlQueryParams.Add(key,value);
            }
		    return urlQueryParams;
	    }
    }
}
